<?php

    namespace app\listen\model;

    use think\Exception;

    class Message extends \app\listen\model\Base {
        public function add($model) {
            return $this->save($model);
        }

        public function lists($recipientUid, $sendUid, $type, $page = 1, $rows = 10) {

            try {
                $createSql = function () use ($recipientUid, $sendUid, $type) {
                    $where = [];
                    if (isset($recipientUid)) {
                        $where ['v.recipient_uid'] = $recipientUid;
                    }
                    if (isset($sendUid)) {
                        $where ['v.send_uid'] = $sendUid;
                    }

                    if (isset($type)) {
                        $where ['v.type'] = $type;
                    }
                    return $this
                        ->alias("v")
                        ->join('listen_member m', 'm.uid=v.send_uid', 'LEFT')
                        ->where($where);
                };

                $total = $createSql()->field('count(1)')->count();
                $lists = $createSql()
                    ->field('v.*, m.nickname,m.head_photo,m.sex')
                    ->order('v.addtime desc')
                    ->page($page, $rows)
                    ->select();
                return [
                    'total'=>$total,
                    'list' => $lists
                ];
            } catch (Exception $e) {
                return false;
            }
        }
    }